<?php

namespace app\api\controller;

use app\admin\model\Image;
use app\admin\model\Villageresources AS VillageresourcesMdl;

/**
 * 村资源接口
 * Class Organization
 */
class Villageresources extends BaseController
{

    /**
     * 获取村资源列表
     *
     * @return \think\response\Json
     */
    public function getResourcesList()
    {
        $param = self::getHttpParam();

        if (empty($param->villageId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'villageId不能为空');
        }
        $start = 0;
        $length = 20;
        if (!empty($param->start)) {
            $start = $param->categoryId;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'villageId' => $param->villageId
        ];
        // 分类
        if (!empty($param->categoryId)) {
            $where['categoryId'] = $param->categoryId;
        }
        // 状态
        if (!empty($param->status)) {
            $where['status'] = $param->status;
        }
        $fields = 'id, name, imgUrl, categoryId, number, numberUnit, price, pricePerUnit, isDiscuss, status';

        $result = VillageresourcesMdl::where($where)->order('createDate DESC')->limit($start, $length)->field($fields)->select();
        $total = VillageresourcesMdl::where($where)->count();

        if (empty($result)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到村图片');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 获取村资源详情
     *
     * @return \think\response\Json
     */
    public function getResourcesDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->resourcesId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'resourcesId不能这空');
        }

        $resourcesId = $param->resourcesId;
        $where = [
            'id' => $resourcesId
        ];
        $fields = 'name, imgUrl, categoryId, number, numberUnit, price, pricePerUnit, isDiscuss, status, createDate, isMortgage, contractYears, address, description';

        $resources = VillageresourcesMdl::where($where)->order('createDate DESC')->field($fields)->find();
        // 图集
        $imagesList = Image::where(['relatedId' => $resourcesId, 'tag' => 'imglist'])->field('imgUrl')->select();
        $resources->imagesList = $imagesList;

        if (empty($resources)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关资源');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $resources);
    }

    /**
     * 获取村资源类型
     *
     * @return \think\response\Json
     */
    public function getResourcesType()
    {
        $category = get_villageresources_category();

        if (empty($category)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到资源类型');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $category);
    }

    /**
     * 获取村资源状态
     *
     * @return \think\response\Json
     */
    public function getResourcesStatus()
    {
        $status = get_villageresources_status();

        if (empty($status)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到资源状态');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $status);
    }

}